/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.skycode.tuto.manager;

import com.skycode.cnx.Conexion;
import com.skycode.tuto.dto.MonitorVideoDto;
import com.skycode.tuto.dto.VideoCommentDto;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 *
 * @author LumiLatop
 */
public class VideoCommentManager implements Serializable {

    public VideoCommentDto inicializar() {
        VideoCommentDto dto = new VideoCommentDto();
        dto.setVideoDto(new MonitorVideoDto());
        dto.setUsuario("");
        dto.setComentario("");
        dto.setAdmin(0);
        dto.setFecha(obtenerFechaActual());
        dto.setUrlImagen("");
        return dto;
    }

    public String obtenerFechaActual() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        return sdf.format(calendar.getTime());
    }

    public List<VideoCommentDto> listarTodos() {
        List<VideoCommentDto> lista = new ArrayList<VideoCommentDto>();
        try {
            Connection cnx = Conexion.getConexionOracle();
            String query = "select * from JTUTO_VIDEO_COMMENT";
            PreparedStatement ps = cnx.prepareStatement(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                VideoCommentDto dto = new VideoCommentDto();
                dto.setId(rs.getLong(1));
                dto.setVideoDto(obtenerVideo(rs.getLong(2)));
                dto.setUsuario(rs.getString(3));
                dto.setComentario(rs.getString(4));
                dto.setAdmin(rs.getInt(5));
                dto.setFecha(rs.getString(6));
                dto.setUrlImagen(rs.getString(7));
                lista.add(dto);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return lista;
    }
    
    public void eliminar(Long id) {
        try {
            Connection cnx = Conexion.getConexionOracle();
            String query = "DELETE FROM JTUTO_VIDEO_COMMENT WHERE ID = " + id;
            PreparedStatement ps = cnx.prepareStatement(query);
            ps.executeQuery();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private MonitorVideoDto obtenerVideo(Long id) {
        MonitorVideoManager monitorVideoManager = new MonitorVideoManager();
        MonitorVideoDto monitorVideoDto = new MonitorVideoDto();
        monitorVideoDto = monitorVideoManager.obtenerPorIdNoLista(id);
        
        return monitorVideoDto;
    }
    
    public void guardar(VideoCommentDto dto) {
        try {
            Connection cnx = Conexion.getConexionOracle();
            String query = "INSERT INTO JTUTO_VIDEO_COMMENT VALUES (?,?,?,?,?,?,?)";
            PreparedStatement ps = cnx.prepareStatement(query);
            ps.setLong(1, generarId(dto));
            ps.setLong(2, dto.getVideoDto().getId());
            ps.setString(3, dto.getUsuario());
            ps.setString(4, dto.getComentario());
            ps.setInt(5, dto.getAdmin());
            ps.setString(6, dto.getFecha());
            ps.setString(7, dto.getUrlImagen());
            ps.executeQuery();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public Long generarId(VideoCommentDto dto) {
        Long maxId = 0L;
        List<VideoCommentDto> lista = listarTodos();
        if (lista != null && lista.size() > 0) {
            for (VideoCommentDto x : lista) {
                if (x.getId().compareTo(maxId) > 0) {
                    maxId = x.getId();
                }
            }
            maxId = maxId + 1;
        } else {
            maxId = 100L;
        }
        return maxId;
    }
    
    public List<VideoCommentDto> obtenerTodosPorId(Long id) {
        List<VideoCommentDto> lista = new ArrayList<VideoCommentDto>();
        try {
            Connection cnx = Conexion.getConexionOracle();
            String query = "select * from JTUTO_VIDEO_COMMENT where VIDEO = " + id;
            PreparedStatement ps = cnx.prepareStatement(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                VideoCommentDto dto = new VideoCommentDto();
                dto.setId(rs.getLong(1));
                dto.setVideoDto(obtenerVideo(rs.getLong(2)));
                dto.setUsuario(rs.getString(3));
                dto.setComentario(rs.getString(4));
                dto.setAdmin(rs.getInt(5));
                dto.setFecha(rs.getString(6));
                dto.setUrlImagen(rs.getString(7));
                lista.add(dto);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return lista;
    }
    
    public List<VideoCommentDto> obtenerTodosPorIdEntidad(Long id) {
        List<VideoCommentDto> lista = new ArrayList<VideoCommentDto>();
        try {
            Connection cnx = Conexion.getConexionOracle();
            String query = "select * from JTUTO_VIDEO_COMMENT where ID = " + id;
            PreparedStatement ps = cnx.prepareStatement(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                VideoCommentDto dto = new VideoCommentDto();
                dto.setId(rs.getLong(1));
                dto.setVideoDto(obtenerVideo(rs.getLong(2)));
                dto.setUsuario(rs.getString(3));
                dto.setComentario(rs.getString(4));
                dto.setAdmin(rs.getInt(5));
                dto.setFecha(rs.getString(6));
                dto.setUrlImagen(rs.getString(7));
                lista.add(dto);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return lista;
    }
}
